package cn.zcj.studytest.service;

import cn.zcj.studytest.ThrowData.Result;
import cn.zcj.studytest.domain.Image;
import cn.zcj.studytest.domain.SignUp;
import cn.zcj.studytest.mapper.InsertContentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class InsertContentService extends ServiceImpl<InsertContentMapper, Image> {
    @Autowired
    private SignUpService signUpService;
    // 上传图片
    public Result unLoadImage(Image image) {
        Result result = new Result();
        Boolean isUnload = this.save(image);
        if(isUnload){
            result.data(image.getId());
            result.ok("上传成功！");
        }else {
            result.setMsg("上传失败");
        }
        return result;
    };
    // 获取图片
    public Result getImage(Image image) {
        Result result = new Result();
        Long userId = image.getUserId();
        List<Image> images = this.lambdaQuery().eq(Image::getUserId, userId).list();

        result.data(images);
        result.setSuccess(true);
        return result;
    }
    // 删除图片
    public Result deleteImg(Image image) {
        Result result = new Result();
        Long userId = image.getUserId();
        Boolean isDelete = this.remove(new QueryWrapper<Image>().eq("id", image.getId()));
        if(isDelete){
            result.setSuccess(true);
            result.setMsg("删除成功");
        }else {
            result.setMsg("删除失败");
        }
        return result;
    }
    // 更新图片
    public Result configImg(Image image) {
        Result result = new Result();
        // 更新图片
        Boolean isUpdate = this.lambdaUpdate()
                .eq(Image::getId, image.getId())
                .set(Image::getIsBuy, image.getIsBuy())
                .set(Image::getPrice, image.getPrice())
                .update();

        if(isUpdate){
            result.setSuccess(true);
            result.setMsg("更新成功");
        }else {
            result.setMsg("更新失败");
        }
        return result;
    }
    // 获取可买卖的图片
    public Result getByImgList(Image image) {
        Result result = new Result();
        List<Image> images = this.lambdaQuery().eq(Image::getIsBuy, image.getIsBuy()).list();
        for (int i = 0; i < images.size(); i++) {
            Long id = images.get(i).getUserId();
            SignUp signUpObj = signUpService.getById(id);
            images.get(i).setUsername(signUpObj.getUsername());
        }
        result.data(images);
        result.setSuccess(true);
        return result;
    }


    public Result buyImg(Image image) {
        Result result = new Result();
        // 获取当前用户
        Long userId = image.getUserId();
        // 获取当前用户 得到他的余额
        SignUp signUpObj = signUpService.getById(userId);
        // 获取当前图片的金额
        Image img = this.getById(image.getId());

        if (signUpObj.getPrice() < img.getPrice()){
            result.setMsg("余额不足");
            result.setSuccess(false);
            return result;
        }
        double price = signUpObj.getPrice() - img.getPrice();
        signUpService.lambdaUpdate()
                .eq(SignUp::getId, userId)
                .set(SignUp::getPrice, price)
                .update();

        // 当前图片的客户id
        Long currentId = img.getUserId();
        // 获取当前图片的客户
        SignUp currentUser = signUpService.getById(currentId);
        double currentPrice = currentUser.getPrice() + img.getPrice();
        signUpService.lambdaUpdate()
                .eq(SignUp::getId, currentId)
                .set(SignUp::getPrice, currentPrice)
                .update();

        // 更新图片用户
        Boolean isBuy = this.lambdaUpdate()
                .eq(Image::getId, image.getId())
                .set(Image::getIsBuy, false)
                .set(Image::getUserId, userId)
                .update();
        if(isBuy){
            result.setSuccess(true);
            result.setMsg("购买成功");
        }
        return result;
    }
}
